package fr.softeam.modelio.task.reporting;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XSLTReportingHandler implements ReportingHandler {
	
	private static final String REPOR_HANDLER_TYPE="XSLT Transformer Handler" ;
	private static final String REPOR_HANDLER_ERR="ERR" ;
	private static final String REPOR_HANDLER_SUCC="SUCCESS" ;
	static Document document;
	private String inputFileName ;
	private String xslFileName ;
	private String outputFileName;
	
	public XSLTReportingHandler(String inputFileName, String xslFileName, String outputFileName){
		this.inputFileName=inputFileName;
		this.xslFileName=xslFileName;
		this.outputFileName =outputFileName;
	}

	@Override
	public ParserHandlerReturn parserApply()  {
		
		ParserHandlerReturn parserHandlerReturn =null;
		try {
				transform();
				parserHandlerReturn = new ParserHandlerReturn.Builder().parseType(REPOR_HANDLER_TYPE).returnType(REPOR_HANDLER_SUCC).parseReturn(this.getOutputFileName()).build();

		} catch (TransformerConfigurationException | SAXException
					| IOException | ParserConfigurationException e) {
			
			parserHandlerReturn = new ParserHandlerReturn.Builder().parseType(REPOR_HANDLER_TYPE).returnType(REPOR_HANDLER_ERR).parseReturn(e.getMessage()).build();
			
		}
		
		return parserHandlerReturn;
	}

	public String getInputFileName() {
		return inputFileName;
	}

	public void setInputFileName(String inputFileName) {
		this.inputFileName = inputFileName;
	}

	public String getXslFileName() {
		return xslFileName;
	}

	public void setXslFileName(String xslFileName) {
		this.xslFileName = xslFileName;
	}

	public String getOutputFileName() {
		return outputFileName;
	}

	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}
	
	/**
	 * This method ...
	 * @throws SAXException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws TransformerConfigurationException
	 */
	private void transform() throws SAXException, IOException, ParserConfigurationException, TransformerConfigurationException   {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		 DocumentBuilder builder = factory.newDocumentBuilder();
         document = builder.parse(new File(this.getInputFileName()));
         TransformerFactory tFactory = TransformerFactory.newInstance();
 
         StreamSource stylesource = new StreamSource(new File(this.getXslFileName())); 
         Transformer transformer = tFactory.newTransformer(stylesource);

	}

}
